//
//  RegisterController.swift
//  SwiftBase
//
//  Created by 郭艳波 on 2018/3/7.
//  Copyright © 2018年 郭艳波. All rights reserved.
//

import UIKit
import SDWebImage

class RegisterController: UIViewController {

    @IBOutlet weak var avatarImage: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setAdaptUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        if self.navigationController != nil {
            self.navigationController!.isNavigationBarHidden = false
        }
    }
    
    @IBAction func onEventUploadImage(_ sender: UIButton) {
        let actionSheet = UIAlertController(title: "上传头像", message: nil, preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        
        let cameraAction = UIAlertAction(title: "拍照", style: .default, handler: {
            (action: UIAlertAction) -> Void in
            //判断是否能进行拍照，可以的话打开相机
            
            let sourceType = UIImagePickerControllerSourceType.camera
            
            if UIImagePickerController.isSourceTypeAvailable(sourceType) {
                let picker = UIImagePickerController.init()
                picker.delegate = self
                picker.allowsEditing = false
                picker.sourceType = sourceType
                picker.showsCameraControls = true
                picker.cameraViewTransform = CGAffineTransform.init(rotationAngle: CGFloat(Double.pi*45/180))
                picker.cameraViewTransform = CGAffineTransform.init(scaleX: 1, y: 1)
                
                self.present(picker, animated: true, completion: nil)
            } else {
                print("模拟其中无法打开照相机, 请在真机中使用")
            }
        })
        
        let photoAction = UIAlertAction(title: "相册", style: .default, handler: {
            (action:UIAlertAction)
            -> Void in
            //调用相册功能，打开相册
            let picker = UIImagePickerController()
            picker.sourceType = .photoLibrary
            picker.delegate = self
            picker.allowsEditing = false
            self.present(picker, animated: true, completion: nil)
            
        })
        
        actionSheet.addAction(cancelAction)
        actionSheet.addAction(cameraAction)
        actionSheet.addAction(photoAction)
        
        self.present(actionSheet, animated: true, completion: nil)
    }
}

extension RegisterController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        picker.dismiss(animated: true, completion: nil)
        
        let img = info[UIImagePickerControllerOriginalImage]
        let imgData = CoreUtil.zipImage(image: img as! UIImage, scale: 0.8, percent: 0.8)
        
        Core.Api.Common.uploadImage(data: imgData)
            .subscribe(
                onNext: { n in
                    let imgUrl = CoreConst.IMG_URL_PREFIX + n.data.file.name
                    self.avatarImage.sd_setImage(with: URL(string: imgUrl), completed: nil)
                },
                onError: { error in
                    print((error as! CoreApiError).message)
                })
            .disposed(by: Core.Api.disposeBag)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
}

// MARK: - UI相关操作
extension RegisterController {
    override func setAdaptUI() {
        
    }
}
